package com.sinsoft.monitorDemo.dao;


import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.ResourceBundle;

import com.sinsoft.monitorDemo.entity.ServerInfo;

public class ServerDao {
	
	public ServerInfo getServerInfo(String code){
		ResourceBundle bundle = ResourceBundle.getBundle("server");	
		String ip = bundle.getString(code);
		switch (code) {
		case "TCMEMR":
			return new ServerInfo("电子病历", code, ip);
		case "TCMAE":
			return new ServerInfo("辨证论治", code, ip);
		case "TCMK":
			return new ServerInfo("知识库", code, ip);
		case "TCMRC":
			return new ServerInfo("远程会诊", code, ip);
		case "TCMRT":
			return new ServerInfo("远程教育", code, ip);
		case "TCMHEAL":
			return new ServerInfo("治未病", code, ip);
		case "TCMCBS":
			return new ServerInfo("业务监管", code, ip);
		case "TCMPMS":
			return new ServerInfo("资源配置", code, ip);
		case "TCMESB":
			return new ServerInfo("ESB", code, ip);
		case "NGINX":
			return new ServerInfo("负载均衡", code, ip);
		case "ORACLE":
			return new ServerInfo("ORACLE", code, ip);
		case "MYSQL":
			return new ServerInfo("MYSQL", code, ip);
		}
		return null;
	}
	
	public List<ServerInfo> getAllServerInfo(){
		Properties prop = new Properties();
		List<ServerInfo> list = new ArrayList<ServerInfo>();
		ServerInfo serverInfo;
		try {
			InputStream in = ServerDao.class.getClassLoader().getResourceAsStream("server.properties");
			prop.load(in);
			Iterator<String> it = prop.stringPropertyNames().iterator();
			while (it.hasNext()) {
				String key = it.next();
				String ip = prop.getProperty(key);
				switch (key) {
				case "TCMEMR":
					serverInfo = new ServerInfo("电子病历", key, ip);
					list.add(serverInfo);
					break;
				case "TCMAE":
					serverInfo = new ServerInfo("辨证论治", key, ip);
					list.add(serverInfo);
					break;
				case "TCMK":
					serverInfo = new ServerInfo("知识库", key, ip);
					list.add(serverInfo);
					break;
				case "TCMRC":
					serverInfo = new ServerInfo("远程会诊", key, ip);
					list.add(serverInfo);
					break;
				case "TCMRT":
					serverInfo = new ServerInfo("远程教育", key, ip);
					list.add(serverInfo);
					break;
				case "TCMHEAL":
					serverInfo = new ServerInfo("治未病", key, ip);
					list.add(serverInfo);
					break;
				case "TCMCBS":
					serverInfo = new ServerInfo("业务监管", key, ip);
					list.add(serverInfo);
					break;
				case "TCMPMS":
					serverInfo = new ServerInfo("资源配置", key, ip);
					list.add(serverInfo);
					break;
				case "TCMESB":
					serverInfo = new ServerInfo("ESB", key, ip);
					list.add(serverInfo);
					break;
				case "NGINX":
					serverInfo = new ServerInfo("负载均衡", key, ip);
					list.add(serverInfo);
					break;
				case "ORACLE":
					serverInfo = new ServerInfo("ORACLE", key, ip);
					list.add(serverInfo);
					break;
				case "MYSQL":
					serverInfo = new ServerInfo("MYSQL", key, ip);
					list.add(serverInfo);
					break;
				}
				
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return list;	
	}
	
	public Map<String, Object> getAllServerInfoByIp(){
		Properties prop = new Properties();
		List<ServerInfo> list = new ArrayList<ServerInfo>();
		List<String> ipList = new ArrayList<String>();
		ServerInfo serverInfo;
		try {
			InputStream in = ServerDao.class.getClassLoader().getResourceAsStream("server.properties");
			prop.load(in);
			Iterator<String> it = prop.stringPropertyNames().iterator();
			while (it.hasNext()) {
				String key = it.next();
				String ip = prop.getProperty(key);
				if (!ipList.contains(ip)) {
					ipList.add(ip);
				}
				switch (key) {
				case "TCMEMR":
					serverInfo = new ServerInfo("电子病历", key, ip);
					list.add(serverInfo);
					break;
				case "TCMAE":
					serverInfo = new ServerInfo("辨证论治", key, ip);
					list.add(serverInfo);
					break;
				case "TCMK":
					serverInfo = new ServerInfo("知识库", key, ip);
					list.add(serverInfo);
					break;
				case "TCMRC":
					serverInfo = new ServerInfo("远程会诊", key, ip);
					list.add(serverInfo);
					break;
				case "TCMRT":
					serverInfo = new ServerInfo("远程教育", key, ip);
					list.add(serverInfo);
					break;
				case "TCMHEAL":
					serverInfo = new ServerInfo("治未病", key, ip);
					list.add(serverInfo);
					break;
				case "TCMCBS":
					serverInfo = new ServerInfo("业务监管", key, ip);
					list.add(serverInfo);
					break;
				case "TCMPMS":
					serverInfo = new ServerInfo("资源配置", key, ip);
					list.add(serverInfo);
					break;
				case "TCMESB":
					serverInfo = new ServerInfo("ESB", key, ip);
					list.add(serverInfo);
					break;
				case "NGINX":
					serverInfo = new ServerInfo("负载均衡", key, ip);
					list.add(serverInfo);
					break;
				case "ORACLE":
					serverInfo = new ServerInfo("ORACLE", key, ip);
					list.add(serverInfo);
					break;
				case "MYSQL":
					serverInfo = new ServerInfo("MYSQL", key, ip);
					list.add(serverInfo);
					break;
				}
				
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
//		System.out.println(list);
		Map<String, Object> map= new HashMap<String, Object>();
		for (String string : ipList) {
			List<ServerInfo> serverInfos = new ArrayList<ServerInfo>();
			for (ServerInfo serverInfo1 : list) {
				if(string.equals(serverInfo1.getIp())){
					serverInfos.add(serverInfo1);
//					System.out.println(serverInfos);
				}
//				System.out.println(string.hashCode());
			}
			map.put(string, serverInfos);
		}
		return map;	
	}
}
